package com.lh.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.lh.entity.EduSubject;
import com.lh.entity.excel.ExcelSubjectData;
import com.lh.service.IEduSubjectService;
import com.lh.servicebase.exceptionhandler.GlobalExceptionHandler;
import com.lh.servicebase.exceptionhandler.MyException;
import org.springframework.transaction.annotation.Transactional;

import java.security.GeneralSecurityException;

/**
 * @author YMS
 * @date 2021/6/5
 */
public class ExcelSubjectListener extends AnalysisEventListener<ExcelSubjectData> {

    //因为ExcelSubjectListener 不能交给spring管理，需要自己new,不能注入其他对象
    public IEduSubjectService eduSubjectService;

    public ExcelSubjectListener(){}

    public ExcelSubjectListener(IEduSubjectService eduSubjectService) {
        this.eduSubjectService = eduSubjectService;
    }

    /**
     * 读取Excel 内容
     * @param excelSubjectData
     * @param analysisContext
     */
    @Override
    public void invoke(ExcelSubjectData excelSubjectData, AnalysisContext analysisContext) {
        if (excelSubjectData ==null){
            throw new MyException(20001,"文件数据为空");
        }

        //生成一级类别
        EduSubject existOneSubject = this.existOneSubject(eduSubjectService, excelSubjectData.getOneSubjectName());
        if (existOneSubject==null){
            existOneSubject=new EduSubject();
            existOneSubject.setParentId("0");
            existOneSubject.setTitle(excelSubjectData.getOneSubjectName());
            eduSubjectService.save(existOneSubject);
        }
        String pid=existOneSubject.getId();
        //生成二级类别
        EduSubject existTwoSubject = this.existTwoSubject(eduSubjectService, excelSubjectData.getTwoSubjectName(), pid);
        if (existTwoSubject==null){
            existTwoSubject=  new EduSubject();
            existTwoSubject.setParentId(pid);
            existTwoSubject.setTitle(excelSubjectData.getTwoSubjectName());
            eduSubjectService.save(existTwoSubject);
        }

    }

    /**
     * 判断一级分类不能重复添加
     * @param eduSubjectService
     * @param name
     * @return
     */
    private EduSubject existOneSubject(IEduSubjectService eduSubjectService,String name){
        LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduSubject::getTitle,name);
        wrapper.eq(EduSubject::getParentId,"0");
        EduSubject oneSubject = eduSubjectService.getOne(wrapper);

        return oneSubject;
    }

    /**
     * 判断二级分类不能重复添加
     * @param
     */
    public EduSubject existTwoSubject(IEduSubjectService eduSubjectService,String pid,String name){
        LambdaQueryWrapper<EduSubject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EduSubject::getTitle,name);
        wrapper.eq(EduSubject::getParentId,pid);
        EduSubject twoSubject = eduSubjectService.getOne(wrapper);
        return twoSubject;

    }


    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
